package com.cqsw.bbs.controller;

import com.cqsw.bbs.entity.Comment;
import com.cqsw.bbs.entity.Forum;
import com.cqsw.bbs.entity.PageView;
import com.cqsw.bbs.entity.Tag;
import com.cqsw.bbs.service.ForumService;
import com.cqsw.bbs.service.TagService;
import com.cqsw.bbs.utils.PageViewUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author: ZackYoung
 * @time: 2020/6/7
 */
@Controller
public class IndexController {
    @Autowired
    private TagService tagService;
    @Autowired
    private ForumService forumService;
    @RequestMapping("index")
    public ModelAndView indexPage(ModelAndView mav,Integer tagId,Integer   page){
        mav.setViewName("index");
        List<Tag> tagList = tagService.queryOnTag("1");
        mav.addObject("tagList",tagList);
        mav.addObject("url_tagId",tagId);
        int totalRecord = forumService.queryForumCount(tagId);
        Forum forum = new Forum();
        PageViewUtil pageViewUtil = new PageViewUtil();
        int limitPage = 8;

        if (tagId!=null){
            forum.setTagId(tagId);
        }
        if (page==null){
            page=1;
        }

        PageView pageView =pageViewUtil.pageView(page,totalRecord,"/index?page=",limitPage);

        List<Forum> forums =forumService.queryForum(forum,( pageView.getCurrentpage()- 1) * limitPage,limitPage);

        mav.addObject("topic",forums);
        mav.addObject("pageView",pageView);
        return mav;
    }
    @RequestMapping("forum")
    public ModelAndView forumPage(ModelAndView mav,Integer topicId,Integer page){
        Forum forum = new Forum();
        if (topicId!=null){
            forum.setId(topicId);
        }
        if (page==null){
            page=1;
        }
        List<Forum> forums =forumService.queryForum(forum,null,null);
        for (Forum f : forums){
            mav.addObject("topic",f);
        }
        int limitPage = 8;
        PageViewUtil pageViewUtil = new PageViewUtil();
        int totalRecord = forumService.queryCommentCount(topicId);
        PageView pageView =pageViewUtil.pageView(page,totalRecord,"/forum?topicId?"+topicId+"page=",limitPage);
        List<Comment> comments =forumService.queryComment(topicId,( pageView.getCurrentpage()- 1) * limitPage,limitPage,totalRecord);
        // 加载评论
        mav.setViewName("public/blank_1");
        mav.addObject("commentList",comments);
        mav.addObject("pageView",pageView);
        mav.addObject("topicId",topicId);
        return mav;
    }
    @RequestMapping("user/editCommentReply")
    public ModelAndView editCommentReply(ModelAndView mav,Integer commentId){
        mav.setViewName("forum/topicRelated_replyComment");
        mav.addObject("replyId",commentId);
        return mav;
    }

}
